package org.dht.com.conf;

import java.io.File;
import java.net.URISyntaxException;
import java.security.CodeSource;
import java.util.HashMap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 * Java class for anonymous complex type. <br>
 * The following schema fragment specifies the expected content contained within
 * this class.
 * 
 * <pre>
 * <complexType>
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element ref="{http://www.DHT-UQ.org/communication}instance"/>
 *         <element ref="{http://www.DHT-UQ.org/communication}time"/>
 *         <element ref="{http://www.DHT-UQ.org/communication}params"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * </pre>
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "instance", "time", "params" })
@XmlRootElement(name = "com")
public class CommunicationProperties {
	/**
	 * Package that contains mapped class to XML
	 */
	private static String PACKAGE = "";

	static {
		String data = CommunicationProperties.class.getCanonicalName();
		int lastIndex = data.lastIndexOf(".");
		PACKAGE = data.substring(0, lastIndex);
	}

	/**
	 * Creates a CommunicationProperties by clazz that invoke and path of file
	 * properties
	 * 
	 * @param clazz
	 *            Class that invoke this method
	 * @param filePath
	 *            Path to file properties
	 * @return CommunicationProperties
	 * @throws CommunicationPropertiesException
	 *             throw when error occur
	 */
	public static CommunicationProperties load(Class<?> clazz, String filePath)
			throws CommunicationPropertiesException {
		try {
			CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
			File jarFile = new File(codeSource.getLocation().toURI().getPath());
			File jarDir = jarFile.getParentFile();
			File propFile = null;

			if (jarDir != null && jarDir.isDirectory())
				propFile = new File(jarDir, filePath);

			JAXBContext jContext = JAXBContext.newInstance(PACKAGE);
			Unmarshaller u = jContext.createUnmarshaller();

			return (CommunicationProperties) u.unmarshal(propFile);
		} catch (JAXBException e) {
			throw new CommunicationPropertiesException("Error reading file "
					+ filePath, e);
		} catch (URISyntaxException e) {
			throw new CommunicationPropertiesException("Error reading file "
					+ filePath, e);
		}
	}

	/**
	 * Object instance
	 */
	@XmlElement(required = true)
	protected Instance instance;
	/**
	 * Object time
	 */
	@XmlElement(required = true)
	protected Time time;
	/**
	 * Params communication
	 */
	@XmlElement(required = true)
	@XmlJavaTypeAdapter(XmlAdapterHashMap.class)
	protected HashMap<String, String> params;

	/**
	 * Gets the value of the instance property.
	 * 
	 * @return possible object is {@link Instance }
	 * 
	 */
	public Instance getInstance() {
		return instance;
	}

	/**
	 * Sets the value of the instance property.
	 * 
	 * @param value
	 *            allowed object is {@link Instance }
	 * 
	 */
	public void setInstance(Instance value) {
		this.instance = value;
	}

	/**
	 * Gets the value of the time property.
	 * 
	 * @return possible object is {@link Time }
	 * 
	 */
	public Time getTime() {
		return time;
	}

	/**
	 * Sets the value of the time property.
	 * 
	 * @param value
	 *            allowed object is {@link Time }
	 * 
	 */
	public void setTime(Time value) {
		this.time = value;
	}

	/**
	 * Gets params
	 * 
	 * @return Params
	 */
	public HashMap<String, String> getParams() {
		return params;
	}

	/**
	 * Sets params
	 * 
	 * @param params
	 */
	public void setParams(HashMap<String, String> params) {
		this.params = params;
	}

}
